home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3734 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.7 KB  |  67 lines

  1. Newsgroups: comp.lang.c++,comp.lang.c
  2. Path: txnews.amd.com!news
  3. From: Bret Patterson <faustus>
  4. Subject: Re: Performance: C vs. C++
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <DLr46y.7rH@txnews.amd.com>
  7. Sender: news@txnews.amd.com
  8. Nntp-Posting-Host: fuggles
  9. Content-Transfer-Encoding: 7bit
  10. Organization: Advanced Micro Devices, Inc., Austin, Texas, USA
  11. References: <30F6BAAC.12B5@iastate.edu> <4da9pn$a45@news.bridge.net> <4dnpl2$c8g@classic.iinet.com.au> <3105E9DC.1BE3@enermet.fi>
  12. Mime-Version: 1.0
  13. Date: Thu, 25 Jan 1996 19:16:09 GMT
  14. X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.05 9000/715)
  15. X-Url: news:3105E9DC.1BE3@enermet.fi
  16.  
  17. Harri Halttunen <Harri.Halttunen@enermet.fi> wrote:
  18. >> Not quite.  C is quite different from C++ from the performance point of
  19. >> view.  The difference is not only in virtual functions and exceptions
  20. >> but in class construction, destruction, etc... and most significantly in
  21. >> Runtime Type Checking.  However, because machines are getting faster by
  22. >> the day, this sort of "inefficiency" is hardly noticable.
  23. >
  24. >All that is true (at least in the some point of view), but we have to remember 
  25. >that usually in real applications this kind of comparison doesn't matter. This 
  26. >is because fetching something from a database or file takes many times longer 
  27. >than, for example, a virtual function call or to handle an excepion. Or even 
  28. >check a type of an object at runtime.
  29.  
  30. Except what most people use virtual functions for is to avoid large switch
  31. statements and make code more easily changed, more readable etc.
  32.  
  33. I would say a*WELL written* C++ program is faster than a *Well written* C  program that does the
  34. exact same thing. Notice *Well written* because most c++ programmers are in their infancy
  35. and blame their bad coding on the language.
  36.  
  37. Here is an example to illustrate why virtual functions do not cause extra overhead.
  38.  
  39. C program:
  40.  
  41. switch (object->type)
  42. {
  43.     case 1 : DragonAttack();
  44.     case 2 : OrcAttack();
  45.     ...
  46. };
  47.  
  48. and a C++:
  49.     object->Attack(); // which internally g++ does a object->virtualTable[Function#]();
  50.  
  51. Big deal. So c++ does a virtual table lookup, but in order for C to have this ability
  52. it has to use a switch statement or ifelse construct. I would say with good compiler
  53. optimization C++ is going to be faster and more readable.
  54.  
  55. Of course C++ uses 4bytes for the pointer while in C you would use a char to represent
  56. type. But if the extra 3 bytes per object is a major problem then you wouldn't want that
  57. extra byte in C either. You would use another method.
  58.  
  59. >I use C++ mainly, because it supports code and idea reuse better than other 
  60. >languages.
  61.  
  62. The only thing of C++ that I know causes alot of overhead is exceptions. But other forms
  63. of error checking are available and exceptions can also be optimized/minimized.
  64.  
  65. Bret
  66.  
  67.